package cn.wolfcode.shiro;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.mapper.RoleMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CrmRealm extends AuthorizingRealm {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    @Autowired  //注入spring容器中的凭证匹配器
    public void setCredentialsMatcher (CredentialsMatcher credentialsMatcher){
        super.setCredentialsMatcher(credentialsMatcher);
    }


    //获取认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //通过token获取到用户名
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        String username = usernamePasswordToken.getUsername();

        //查询数据库中是否存在该用户名
        Employee employee = employeeMapper.checkEmployeeByName(username);
        if (employee != null) {
            //如果存在，就把用户的用户名,密码封装到AuthenticationInfo中，最后返回给shiro使用
            //第一个参数是用户名，第二个参数是用户真正的密码，第三个参数是盐，第四个参数是当前数据源的名字
            return new SimpleAuthenticationInfo(employee, employee.getPassword(), ByteSource.Util.bytes(employee.getName()), this.getName());
        }
        return null;
    }

    //获取授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //获取当前登录用户的对象
        //两个方法都可以拿到登录对象的所有信息，前提是在获取认证的时候传入对象
        Subject subject = SecurityUtils.getSubject();
        Employee principal = (Employee) subject.getPrincipal();
        //Employee principal =(Employee) principals.getPrimaryPrincipal();
        //info对象中的数据就是当前登录用户的权限和角色的信息
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        if (!principal.isAdmin()) {
            //根据员工ID查询员工的角色
            List<String> roles = roleMapper.selectSnByEmpId(principal.getId());
            info.addRoles(roles);
            //根据员工id查询员工的权限表达式集合
            List<String> expressions = permissionMapper.selectExpressionByEmpId(principal.getId());
            //将权限存进info中
            info.addStringPermissions(expressions);
        } else {
            info.addStringPermission("*:*");//拥有所有权限
            info.addRole("ADMIN");//管理员角色，可以不给
        }
        return info;
    }
}
